from crv.tools.clustermgr import *
from crv.model.crvmodel import *

from crv.model import DBSession, metadata

import transaction
import random

from crv.config.environment import load_environment
from crv import model
from crv.model.crvmodel import *        
import time

from paste.deploy import appconfig
from pylons import config

conf = appconfig('config:development.ini', relative_to='.')
load_environment(conf.global_conf, conf.local_conf)

nRoles=5

#initialized
cluster=Cluster("cluster")
for i in range(nRoles):
    irole=Role("role%d"%i)
    cluster.role.append(irole)
    
checkStateFuncs=[checkState_INITIALIZED, checkState_STARTING, checkState_STARTED, checkState_STOPPING, checkState_EXCEPTION, checkState_FIXING]

nTests=100
for i in range(nTests):
    #randomly set states of the roles
    #print "role.currentSC=",
    for irole in cluster.role:
        irole.currentSC=random.randint(0,Role.SC.nSC-1)
        #print Role.SCname[irole.currentSC],
    
    #run the functions
    testresult=[]
    for iFunc in checkStateFuncs:
        if iFunc(cluster):
            testresult.append(1)
        else:
            testresult.append(0)
    
    #print "TestResult=",testresult
    
    if sum(testresult)>1:
        print "role.currentSC=",
        for irole in cluster.role:
            print Role.SCname[irole.currentSC],
        print "TestResult=",testresult

